﻿using System;
using System.ComponentModel;
using Loki.Utils;

namespace Luna.Model.CRM
{
    internal class EditableAccount : Account, IEditableAccount, IAccount
    {
        public EditableAccount()
        {
            Name = "Nouveau compte";
        }

        public EditableAccount(Account account)
            : base(account)
        {
            BeginInit();

            Name = account.Name;
            Category = account.Category;
            Address = account.Address;
            ZipCode = account.ZipCode;
            City = account.City;
            Country = account.Country;
            Url = account.Url;
            Comment = account.Comment;

            EndInit(!IsChanged);
        }

        #region Category

        private static PropertyChangedEventArgs argsCategoryChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.Category);

        private static PropertyChangingEventArgs argsCategoryChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.Category);

        public new ICategory Category
        {
            get
            {
                return GetEntity(ApplicationModel.CRM.Categories, Data.CategoryID);
            }

            set
            {
                var bufferID = value == null ? null : (Guid?)value.ID;
                if (bufferID != Data.CategoryID)
                {
                    NotifyChanging(argsCategoryChanging);
                    Data.CategoryID = bufferID;
                    NotifyChangedAndDirty(argsCategoryChanged);
                }
            }
        }

        #endregion Category

        #region Name

        private static PropertyChangedEventArgs argsNameChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.Name);

        private static PropertyChangingEventArgs argsNameChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.Name);

        public new string Name
        {
            get
            {
                return Data.Name;
            }

            set
            {
                if (value != Data.Name)
                {
                    NotifyChanging(argsNameChanging);
                    Data.Name = value;
                    NotifyChangedAndDirty(argsNameChanged);
                }
            }
        }

        #endregion Name

        #region Address

        private static PropertyChangedEventArgs argsAddressChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.Address);

        private static PropertyChangingEventArgs argsAddressChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.Address);

        public new string Address
        {
            get
            {
                return Data.Address;
            }

            set
            {
                if (value != Data.Address)
                {
                    NotifyChanging(argsAddressChanging);
                    Data.Address = value;
                    NotifyChangedAndDirty(argsAddressChanged);
                }
            }
        }

        #endregion Address

        #region ZipCode

        private static PropertyChangedEventArgs argsZipCodeChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.ZipCode);

        private static PropertyChangingEventArgs argsZipCodeChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.ZipCode);

        public new string ZipCode
        {
            get
            {
                return Data.ZipCode;
            }

            set
            {
                if (value != Data.ZipCode)
                {
                    NotifyChanging(argsZipCodeChanging);
                    Data.ZipCode = value;
                    NotifyChangedAndDirty(argsZipCodeChanged);
                }
            }
        }

        #endregion ZipCode

        #region City

        private static PropertyChangedEventArgs argsCityChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.City);

        private static PropertyChangingEventArgs argsCityChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.City);

        public new string City
        {
            get
            {
                return Data.City;
            }

            set
            {
                if (value != Data.City)
                {
                    NotifyChanging(argsCityChanging);
                    Data.City = value;
                    NotifyChangedAndDirty(argsCityChanged);
                }
            }
        }

        #endregion City

        #region Country

        private static PropertyChangedEventArgs argsCountryChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.Country);

        private static PropertyChangingEventArgs argsCountryChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.Country);

        public new string Country
        {
            get
            {
                return Data.Country;
            }

            set
            {
                if (value != Data.Country)
                {
                    NotifyChanging(argsCountryChanging);
                    Data.Country = value;
                    NotifyChangedAndDirty(argsCountryChanged);
                }
            }
        }

        #endregion Country

        #region Url

        private static PropertyChangedEventArgs argsUrlChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.Url);

        private static PropertyChangingEventArgs argsUrlChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.Url);

        public new Uri Url
        {
            get
            {
                return new Uri(Data.Url);
            }

            set
            {
                string buffer = value == null ? string.Empty : value.ToString();
                if (buffer != Data.Url)
                {
                    NotifyChanging(argsUrlChanging);
                    Data.Country = buffer;
                    NotifyChangedAndDirty(argsUrlChanged);
                }
            }
        }

        #endregion Url

        #region Comment

        private static PropertyChangedEventArgs argsCommentChanged = ObservableHelper.CreateChangedArgs<EditableAccount>(x => x.Comment);

        private static PropertyChangingEventArgs argsCommentChanging = ObservableHelper.CreateChangingArgs<EditableAccount>(x => x.Comment);

        public new string Comment
        {
            get
            {
                return Data.Comment;
            }

            set
            {
                if (value != Data.Comment)
                {
                    NotifyChanging(argsCommentChanging);
                    Data.Comment = value;
                    NotifyChangedAndDirty(argsCommentChanged);
                }
            }
        }

        #endregion Comment
    }
}